package com.study.mina;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.SocketSessionConfig;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetSocketAddress;

/**
 * Description:
 * User: 27010
 * DateTime: 2018/5/13 10:52
 */
public class Server {
    private static final Logger LOGGER = LoggerFactory
            .getLogger(Server.class);
    private static int port =  8080;

    public static void main(String[] args) {
        SocketAcceptor acceptor = new NioSocketAcceptor();
        // 定义每次接收数据大小
        SocketSessionConfig sessionConfig = acceptor.getSessionConfig();
        //receive read send buffersize
        sessionConfig.setReadBufferSize(2048);
        sessionConfig.setThroughputCalculationInterval(100);

        DefaultIoFilterChainBuilder filterChain = acceptor.getFilterChain();
        filterChain.addLast("cdec",new ProtocolCodecFilter(new TextLineCodecFactory()));
        acceptor.setHandler(new SimpleServerHandler());

        try {
            acceptor.bind(new InetSocketAddress(port));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
